
//	#include "IO.h"
//	#include "gd32f30x_spi.h"

#include "BSP.h"
#include "BSP_Config.h"

#if 0
static const IO_T W25Q_CS = IO_INIT_TABLE(GPIOB, GPIO_PIN_12);
static const IO_T W25Q_SPI_SCK = IO_INIT_TABLE(GPIOB, GPIO_PIN_13);
static const IO_T W25Q_SPI_MISO = IO_INIT_TABLE(GPIOB, GPIO_PIN_14);
static const IO_T W25Q_SPI_MOSI = IO_INIT_TABLE(GPIOB, GPIO_PIN_15);

/*!
    \brief      configure the SPI peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void W25Q_SPI_Config(void)
{
    spi_parameter_struct spi_init_struct;
	
    rcu_periph_clock_enable(RCU_SPI1);
	
    /* deinitilize SPI and the parameters */
    spi_i2s_deinit(SPI1);
//	    spi_struct_para_init(&spi_init_struct);

    /* SPI1 parameter config */
    spi_init_struct.trans_mode           = SPI_TRANSMODE_FULLDUPLEX;
    spi_init_struct.device_mode          = SPI_MASTER;
    spi_init_struct.frame_size           = SPI_FRAMESIZE_8BIT;
    spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
    spi_init_struct.nss                  = SPI_NSS_SOFT;
    spi_init_struct.prescale             = SPI_PSC_64;
    spi_init_struct.endian               = SPI_ENDIAN_MSB;
    spi_init(SPI1, &spi_init_struct);
    
    spi_nss_internal_high(SPI1);
    spi_enable(SPI1);
}

static void W25Q_GPIO_Init(void){
	IO_Init(&W25Q_SPI_SCK, IO_Mode_AFPP);
	IO_Init(&W25Q_SPI_MOSI, IO_Mode_AFPP);
	IO_Init(&W25Q_SPI_MISO, IO_Mode_IN);
	
	IO_Init(&W25Q_CS, IO_Mode_OUT);
	IO_Set(&W25Q_CS, 1);
}
#endif 


void W25Q_IO_Init(void){
	BSP_SPI_Init(W25_SpiId, 15 * 1000000);
}

uint8_t W25Q_TxRxByte(uint8_t TxByte)
{
	return BSP_SPI_Exchange(W25_SpiId, TxByte);
#if 0
	  while(RESET == spi_i2s_flag_get(SPI1, SPI_FLAG_TBE));
	  spi_i2s_data_transmit(SPI1, TxByte);
	  while(RESET == spi_i2s_flag_get(SPI1, SPI_FLAG_RBNE));
	  return (unsigned char)spi_i2s_data_receive(SPI1);
#endif 
}



void W25Q_ChipSelect(unsigned int PinLevel){
	BSP_SPI_SetNSS(W25_SpiId, PinLevel);
}


static void W25W_DelayUnit(void){
	volatile uint32_t i;
	for(i = 0; i < 10; i++){

	}
}

static volatile unsigned int w25qTimeoutCount;

void W25Q_TimeoutStart(unsigned int waitTime){
	w25qTimeoutCount = waitTime;
}


int W25Q_IsTimeout(void){
	if(w25qTimeoutCount > 0) {
		w25qTimeoutCount--;
		W25W_DelayUnit();
		return 0;
	}
	return 1;
}



